package com.skyz.base.manager;

import android.app.Activity;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.hjq.toast.ToastUtils;
import com.skyz.base.dialog.EnvironmentDialogFragment;
import com.skyz.base.dialog.SecretDoorPasswordDialogFragment;

public class SecretDoorManager {
    private final String TAG = getClass().getSimpleName();

    private static final int CLICK_MODE_SHOW_COUNT = 8;
    private static final String SECRET_DOOR_PASSWORD = "hk@123";

    private CountDownTimer mCountDownTimer;

    private int mClickDoorCount = 0;

    private SecretDoorManager() {
    }

    private static class InstanceHolder {
        private static final SecretDoorManager instance = new SecretDoorManager();
    }

    public static SecretDoorManager getInstance() {
        return InstanceHolder.instance;
    }

    public void attachView(View view) {
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickMode();
            }
        });
    }

    private void clickMode() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
        }
        mCountDownTimer = new CountDownTimer(1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                mClickDoorCount = 0;
                mCountDownTimer = null;
                Log.i(TAG, "timeout and reset click door count");
            }
        };
        mCountDownTimer.start();
        mClickDoorCount++;
        Log.i(TAG, "ClickDoorCount : " + mClickDoorCount);
        if (mClickDoorCount >= CLICK_MODE_SHOW_COUNT) {
            mCountDownTimer.cancel();
            mClickDoorCount = 0;
            showSecretDoor();
            Log.i(TAG, "show secret door and reset click door count");
        }
    }

    private void showSecretDoor() {
        AppCompatActivity currentActivity = getCurrentActivity();
        if (currentActivity == null) {
            return;
        }
        SecretDoorPasswordDialogFragment.showDialogFragment(currentActivity.getSupportFragmentManager(), null, new SecretDoorPasswordDialogFragment.OnActionClickListener() {
            @Override
            public boolean onConfirmClick(String password) {
                if (TextUtils.equals(password, SECRET_DOOR_PASSWORD)) {
                    showEnvironment();
                    return true;
                }
                ToastUtils.show("password error!");
                return false;
            }
        });
    }

    private void showEnvironment() {
        AppCompatActivity currentActivity = getCurrentActivity();
        if (currentActivity == null) {
            return;
        }
        EnvironmentDialogFragment.showDialogFragment(currentActivity.getSupportFragmentManager(), null);
    }


    private AppCompatActivity getCurrentActivity() {
        Activity activity = ActivityManager.getInstance().currentActivity();
        if (!(activity instanceof AppCompatActivity)) {
            Log.w(TAG, "appCompatActivity can show loading!");
            return null;
        }
        return (AppCompatActivity) activity;
    }
}
